package com.apigateway.controller;

import com.apigateway.mapper.ApiServiceCallMapper;
import com.apigateway.mapper.ApiServiceMapper;
import com.apigateway.model.ApiService;
import com.apigateway.model.ApiServiceCall;
import com.fasterxml.jackson.databind.deser.Deserializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 小泽玛丽罗 on 2017/3/15.
 */
@RestController
public class ServiceRestController extends BaseController{
    @Autowired
    private ApiServiceCallMapper apiServiceCallMapper;

    @Autowired
    private ApiServiceMapper apiServiceMapper;


    @RequestMapping(method = RequestMethod.GET,value = "serviceCost")
    public Map<String, Object> getServiceCost(@RequestParam(value = "serviceId",required =false) int serviceId){
        if(serviceId!=0){
            List<Integer> costs=new ArrayList<>();
            List<ApiServiceCall> serviceCalls=apiServiceCallMapper.findServiceCallById(serviceId);
            if(serviceCalls.size()>0){
                put("serviceCalls",serviceCalls);
                ApiService apiService=apiServiceMapper.getServiceById(serviceId);
                put("service",apiService);
                for(ApiServiceCall apiServiceCall:serviceCalls){
                    costs.add(apiServiceCall.getCosttime());
                }
                put("caost",costs);
                putSuccess();
            }else{
                putFalse();
                putMessage("没有该serviceId");
            }
        }else{
            putMessage("serviceId不能空");
        }
        return anwser;
    }
    @RequestMapping(method = RequestMethod.GET,value = "getServiceNode")
    public Object getServiceNode(@RequestParam(value = "serviceId",required = false) int serviceId){
        if(serviceId>0){
           ApiService apiService= apiServiceMapper.getServiceById(serviceId);
           if (apiService!=null)
               return apiService;
        }
        return null;
    }

}
